#include <stdio.h>
#include <opencv2/opencv.hpp>
#include <fstream>
#include <iostream>
#include <armadillo>
#include <iomanip>
#include <vector>

using namespace std;
using namespace arma;

#include "multi_action_def.hpp"
#include "multi_action_impl.hpp"



//Image Size
uword ro = 144;
uword co = 180;

//Scale bb
//uword ro = 64;
//uword co = 32;

//uword ro = 128;
//uword co = 64;


//Number of samples used per training and testing
uword n_train = 8;
uword n_test  = 1;




//NICTA-QUT
const std::string single_path = "/home/johanna/codes/datasets_codes/weizmann/original/"; 
const std::string multi_path = "/home/johanna/codes/datasets_codes/weizmann/stitched_original/";

//NICTA-QUT Mask datset
const std::string single_path_mask = "/home/johanna/codes/datasets_codes/weizmann/mask/"; 
const std::string multi_path_mask = "/home/johanna/codes/datasets_codes/weizmann/stitched_mask/";

//home
//const std::string single_path = "/media/sdb1/codes/datasets_codes/weizmann/original/"; 
//const std::string multi_path = "/media/sdb1/codes/datasets_codes/weizmann/stitched_original/";

//home-Mask Dataset
//const std::string single_path_mask = "/media/sdb1/codes/datasets_codes/weizmann/mask/"; 
//const std::string multi_path_mask = "/media/sdb1/codes/datasets_codes/weizmann/stitched_mask/";



const std::string  actionList = "action_list.txt";
const std::string  peopleList = "people_list.txt";

void
rand_split(int run)
{
  
  field<string> people;
  people.load(peopleList);
  uword num_peo = people.n_rows;
  //arma_rng::set_seed_random();
  
  field<string> rand_people_train(num_peo - 1 );
  field<string> rand_people_test(1);
  rand_people_test(0) = people (run - 1);
  
  int k=0;
  for (uword i=0; i<num_peo; ++i)
  {
    if (i!=run-1)
    {
      rand_people_train(k) = people(i);
      k++;
    }
    
  }
  std::stringstream train_list;
  train_list<< "./run" << run << "/train_list_run"<< run << ".dat";
  
  std::stringstream test_list;
  test_list<< "./run" << run << "/test_list_run"<< run << ".dat";
  
  //rand_people_train.print("train_list");
  //rand_people_test.print("test_list");
  
  rand_people_train.save( train_list.str() );
  rand_people_test.save( test_list.str() );
  
}




int
main(int argc, char** argv)
{
  
   if(argc < 3)
  {
    cout << "usage: " << argv[0] << " Ng L" << endl;
    return -1;
  }
  
  int N_cent = atoi(argv[1]);
  int L = atoi(argv[2]);
  
  ///Agregar cada Run
  ///Hacer lista de Acciones y de Personas
  ///Entrenar con 8 personas (single actions) y probar con una
  
  field<string> people;
  people.load(peopleList);
  //people.print("All People");
  

  cout << "Multi Action Classification " << endl;
  cout << "Testing for GMM with " << N_cent << " centroids" << endl;
  cout << "L= " << L << endl;
  cout << "Rescaling " << ro << "x" << co << endl;
  
  for (int r=1; r<=people.n_rows; r++)//people.n_rows
  {
    int run = r;
    
    cout << "****************************************" << endl;
    cout << "RUN: " << run << endl;
    cout << "****************************************" << endl;
    
    rand_split(run);
    
    field<string> peo_train;
    field<string> peo_test;
    std::stringstream train_list;
    train_list<< "./run" << run << "/train_list_run"<< run << ".dat";
    std::stringstream test_list;
    test_list<< "./run" << run << "/test_list_run"<< run << ".dat";
    
    peo_train.load( train_list.str() );
    peo_test.load(  test_list.str() );
    
    
 
    

    multi_action multi_action(single_path, multi_path, single_path_mask, multi_path_mask,  actionList, co, ro, run);
    //cout << arma_version::as_string() << endl;
    
    
    cout << "Features Training" << endl;
    //multi_action.features_train( peo_train ); 
    cout << "Features Testing" << endl;
    //multi_action.features_multitest( peo_test ); 
    //multi_action.features_multitest( people ); ///para ver todos los videos
    cout << "Training Model" << endl;
    //multi_action.train_gmm_model( N_cent ); 
    cout << "Evaluating Model" << endl;
    multi_action.test_gmm_model( N_cent, peo_test, L );
  }
  
}



